package com.example.scale;

import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.adt.color.Color;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class Laser extends Rectangle {

	public Turret mTurret;
	public boolean laserHit;
	public final FixtureDef areaFixtureDef = PhysicsFactory.createFixtureDef(0,
			0, 0);
	public final float startRotation = 0;
	public final float stopRotation = (float) Math.toRadians(-45);
	public Body laserBody;
	public Boolean destroyBody;
	public float incrementAmount = 1.0f;

	public Laser(float pX, float pY, float pWidth, float pHeight,
			VertexBufferObjectManager mvbom, Turret turret) {
		super(pX, pY, pWidth, pHeight, mvbom);

		/*setAlpha(0.2f);
		areaFixtureDef.isSensor = true;
		setRotationCenter(1f, 0.5f);	// Have the laser rotate with the same center as the turret
		setRotation(startRotation);

		destroyBody = false;
		laserHit = false;

		mTurret = turret;
		setColor(Color.RED);
		laserBody = PhysicsFactory.createBoxBody(GameScene.physicsWorld, this,
				BodyType.KinematicBody, areaFixtureDef);
		laserBody.setUserData(this);
		GameScene.physicsWorld.registerPhysicsConnector(new PhysicsConnector(
				this, laserBody, true, true) {
			@Override
			public void onUpdate(float pSecondsElapsed) {
				super.onUpdate(pSecondsElapsed);
				rotate();
			}
		});*/
	}

	public void rotate() {
		/*
		 * if(laserBody.getAngle() >= startRotation) {
		 * laserBody.setAngularVelocity(-0.6f); } else if (laserBody.getAngle()
		 * <= stopRotation) { laserBody.setAngularVelocity(0.6f); }
		 */
		if (laserBody.getAngle() == startRotation) {
			registerEntityModifier(new RotationModifier(3, startRotation,
					stopRotation));
			// laserBody.setTransform(new Vector2(mLaser.getX(), mLaser.getY()),
			// mLaser.getRotation());
		} else if (laserBody.getAngle() == stopRotation) {
			registerEntityModifier(new RotationModifier(3, stopRotation,
					startRotation));
			// laserBody.setTransform(new Vector2(mLaser.getX(), mLaser.getY()),
			// mLaser.getRotation());
		}
		if (laserHit) {
			laserHit = false;
			mTurret.fireBullet();
		}

		/*if (destroyBody) {
			final PhysicsConnector pc = GameScene.physicsWorld
					.getPhysicsConnectorManager().findPhysicsConnectorByShape(
							this);
			GameScene.physicsWorld.unregisterPhysicsConnector(pc);
			GameScene.physicsWorld.destroyBody(laserBody);
			final Body newLaserBody = PhysicsFactory.createBoxBody(
					GameScene.physicsWorld, this, BodyType.KinematicBody,
					areaFixtureDef);
			newLaserBody.setUserData(this);
			GameScene.physicsWorld
					.registerPhysicsConnector(new PhysicsConnector(this,
							newLaserBody, true, true) {
						@Override
						public void onUpdate(float pSecondsElapsed) {
							rotate();
						}
					});
			destroyBody = false;
		} /*else {
			destroyBody = true;
		}*/
	}
}
